//
//  BindBankCardVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/1/28.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "BindBankCardVC.h"
#import "BindBankDoneVC.h"
#import "MyVM.h"
@interface BindBankCardVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textName;
@property (weak, nonatomic) IBOutlet UITextField *textCard;
@property (weak, nonatomic) IBOutlet UIButton *btnSubmit;
@property (weak, nonatomic) IBOutlet UIImageView *bankIcon;
@property (weak, nonatomic) IBOutlet UILabel *bankName;
@property (weak, nonatomic) IBOutlet UILabel *bankContent;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutHeight;
@property (nonatomic ,strong) BankModel *model;
@property (nonatomic ,strong) NSMutableArray *bankArr;
@property (weak, nonatomic) IBOutlet UILabel *labPrompt;

@end

@implementation BindBankCardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"绑定银行卡";
    self.labPrompt.text=@"*银行卡号必须与姓名保持一致\n\n*目前只支持 工商银行/中国银行/建设银行/农业银行/邮政储蓄银行/交通银行/光大银行/招商银行/民生银行/兴业银行/中信银行/广发银行/华夏银行/浦发银行/平安银行"; self.btnSubmit.layer.masksToBounds=YES;
    self.btnSubmit.layer.cornerRadius=2.0;
    self.textCard.delegate=self;
    _bankIcon.layer.masksToBounds=YES;
    _bankIcon.layer.cornerRadius=20;
    _bankIcon.hidden=YES;
    _layoutHeight.constant=0.0;
    WS(weakSelf);
    [[MyVM manage] getBankCardWithUserId:[UserInfoModel share].Id andSuccess:^(id returnValue) {
        if (returnValue) {
            weakSelf.model=returnValue;
        }else{
            weakSelf.model=[[BankModel alloc]init];
            weakSelf.model.UserId=[UserInfoModel share].Id;
        }
        
        weakSelf.textName.text=weakSelf.model.TrueName;
        [weakSelf updateUI];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)updateUI{
    if (self.model.BankName.length>0) {
        self.textCard.text=self.model.BankAccount;
        _bankIcon.hidden=NO;
        _bankIcon.image=[[self getBankWithName:self.model.BankName] objectForKey:@"img"];
        _bankName.text=self.model.BankName;
        _bankContent.text=[NSString stringWithFormat:@"尾号%@ 储蓄卡",[self.model.BankAccount substringWithRange:NSMakeRange(self.model.BankAccount.length-4, 4)]];
        
        _layoutHeight.constant=60.0;
    }else{
        _bankIcon.hidden=YES;
        _bankName.text=nil;
        _bankContent.text=nil;
        _layoutHeight.constant=0.0;
    }
}


- (IBAction)onclickSubmit:(UIButton *)sender {
    if (self.model.BankAccount.length<15) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的银行卡号"];
        return;
    }else if (self.textName.text.length==0){
        [SVProgressHUD showErrorWithStatus:@"请输入正确的姓名"];
        return;
    }
    self.model.UserId=[UserInfoModel share].Id;
    self.model.TrueName=self.textName.text;
    NSDictionary *dict=[self.model mj_keyValues];
    WS(weakSelf);
    [[MyVM manage] addBankCardWithDict:dict andSuccess:^(id returnValue) {
        BindBankDoneVC *vc=[[UIStoryboard storyboardWithName:@"My" bundle:nil] instantiateViewControllerWithIdentifier:@"BindBankDoneVC"];
        vc.model=weakSelf.model;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
    
    
    
}


- (NSDictionary *)getBankWithName:(NSString *)name{
    for (NSDictionary *dict in self.bankArr) {
        if ([[dict objectForKey:@"name"] isEqualToString:name]) {
            return dict;
        }else if ([[dict objectForKey:@"alias"] isEqualToString:name]){
            return dict;
        }
    }
    return nil;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *str=textField.text;
    if ([string isEqualToString:@""]) {
        str=[str substringWithRange:NSMakeRange(0, str.length-1)];
    }else{
        if (str.length>18) {
            return NO;
        }
        str=[NSString stringWithFormat:@"%@%@",str,string];
    }
    if (str.length>14) {
        WS(weakSelf);
        [[BaseVM manage] getBanInfoWithCardId:str successBlock:^(id returnValue) {
            NSDictionary *dict=returnValue;
            NSString *strBack=[dict objectForKey:@"bank"];
            NSDictionary *bank=[weakSelf getBankWithName:strBack];
            if (bank) {
                weakSelf.model.BankName=[bank objectForKey:@"name"];
                weakSelf.model.BankAccount=str;
            }else{
                weakSelf.model.BankName=nil;
                weakSelf.model.BankAccount=nil;
            }
            
            [weakSelf updateUI];
        } failBlock:^(NSString *errorMessage, NSError *error) {
            
        }];
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    self.model.BankName=nil;
    self.model.BankAccount=nil;
    [self updateUI];
    return YES;
}


#pragma mark -- 懒加载
- (NSMutableArray *)bankArr{
    if (!_bankArr) {
        
        _bankArr=[MyVM manage].bankArr;
    }
    return _bankArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
